package com.donzell.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.donzell.auth.SessionHelper;
import com.donzell.comm.CommControllerHelper;
import com.donzell.dao.UserRepository;
import com.donzell.exception.LoginErrorException;
import com.donzell.model.bean.User;
import com.donzell.model.bean.UserSession;


@Controller
@RequestMapping(value="/login", produces = "application/json;charset=UTF-8")
public class LoginController {
	Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private UserRepository userRepository;
	
	@RequestMapping("tologin")
	public String dispatcherToTypeUser(ModelMap map) {
		map.addAttribute("name", "hh2");
    	map.addAttribute("bookTitle", "tt2");
		return "login/login";
	}
	
	@RequestMapping("login")
	public ModelAndView login(HttpSession httpSession,ModelMap map, String username, String password) {
		logger.info("登录器===>username="+username+"|password="+password);
		
		User olduser = userRepository.findByUsername(username);
		if(olduser == null){
			CommControllerHelper.bindErrMsgs(map, "300", "该账号还未注册！", "/login/tologin", "/");
			return new ModelAndView("error");
		}
		if(!DigestUtils.md5Hex(password).equals(olduser.getPassword())){
			CommControllerHelper.bindErrMsgs(map, "300", "密码错误！", "/login/tologin", "/");
			return new ModelAndView("error");
		}
		
		UserSession userSession = new UserSession();
		userSession.setUsername(username);
		userSession.setPassword(password);
		userSession.setUser(olduser);
		userSession.setLoginTime(new Date());
		UserSession.setUserSession(httpSession, userSession);
		
		return new ModelAndView("redirect:/");
	}
	
	@RequestMapping("toregist")
	public String toRegistView( ) {
		return "login/regist";
	}
	
	@RequestMapping("doregist")
	public String doRegist(ModelMap map, String username, String password) {
		logger.info("登录器doregist===>username="+username+"|password="+password +"|"+DigestUtils.md5Hex(password)+"|"+UUID.randomUUID().toString().length() );
			;
//		if (!Arrays.equals(user.getPassword(), pw)) {
//			throw new RuntimeException("密码错误");
//		}
		int olduser = userRepository.countByUsername(username);
		logger.info("登录器doregist1"+olduser);
		if(olduser > 0){
			CommControllerHelper.bindErrMsgs(map, "300", "该账号已经注册！", "/login/toregist", "/");
			return "err";
		}
		
		User user = userRepository.save(new User(UUID.randomUUID().toString(),username, DigestUtils.md5Hex(password)));
		logger.info("登录器doregist"+user);
		
		String err_message = user != null ? user.getUsername()+",注册账号成功!" : user.getUsername()+",注册账号失败!";
		if(user != null){
			CommControllerHelper.bindSuccMsg(map, err_message , "/", "/");
		}else{
			CommControllerHelper.bindSuccMsg(map, err_message , "/login/toregist", "/");
		}
		return "err";
	}

}
